package com.rxt.thermopro.ui.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.util.SparseArray
import androidx.core.util.forEach
import androidx.core.util.set
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.app.common.base.BaseFragment
import com.app.common.ex.snackBar
import com.rxt.thermopro.R
import com.rxt.thermopro.app.AppContext
import com.rxt.thermopro.app.LocalDataDao
import com.rxt.thermopro.bean.EventAlarmTextData
import com.rxt.thermopro.bean.ProbeBean
import com.rxt.thermopro.bean.ProfileBean
import com.rxt.thermopro.viewmodel.BleMessageViewModel
import com.rxt.thermopro.viewmodel.DeviceStatusViewModel
import com.rxt.thermopro.viewmodel.EventViewModel
import com.rxt.thermopro.viewmodel.TP960ViewModel
import kotlinx.android.synthetic.main.fragment_tp960.*


class TP960Fragment : BaseFragment<TP960ViewModel>() {

    override fun layoutId(): Int {
        return R.layout.fragment_tp960
    }

    companion object {
        private const val ARG_DEVICE_NAME = "deviceName"
        private const val ARG_DEVICE_ADDRESS = "address"
        private const val ARG_DEVICE_PROBE_STATUS = "probeStatus"

        private const val TARGET_TEMP = 1
        private const val TEMP_NONE = 2

        fun newInstance(deviceName: String, address: String): TP960Fragment {
            return TP960Fragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_DEVICE_NAME, deviceName)
                    putString(ARG_DEVICE_ADDRESS, address)
                }
            }
        }

        fun newInstance(deviceName: String, address: String, probeStatus: Boolean): TP960Fragment {
            return TP960Fragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_DEVICE_NAME, deviceName)
                    putString(ARG_DEVICE_ADDRESS, address)
                    putBoolean(ARG_DEVICE_PROBE_STATUS, probeStatus)
                }
            }
        }
    }

    private var mData = ProbeBean()
    private var probeStatus = false
    private lateinit var mDeviceStatusViewModel: DeviceStatusViewModel
    private lateinit var mBleViewModel: BleMessageViewModel
    private lateinit var mEventViewModel: EventViewModel
    private val profileArray = SparseArray<ProfileBean>(2)
    private val probeArray = mutableListOf<ProbeBean>()
    private val refreshTempDataCache = mutableListOf<ProbeBean>()

    override fun onResume() {
        super.onResume()
        if (refreshTempDataCache.isNotEmpty()) {
            mBleViewModel.notifyMsgData.value =
                hashMapOf<Long, MutableList<ProbeBean>>().apply {
                    this[System.currentTimeMillis()] = mutableListOf<ProbeBean>().apply {
                        this.addAll(refreshTempDataCache)
                    }
                }
        }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDeviceStatusViewModel =
            ViewModelProvider(requireActivity()).get(DeviceStatusViewModel::class.java)
        mBleViewModel =
            ViewModelProvider(requireActivity()).get(BleMessageViewModel::class.java)
        mEventViewModel = ViewModelProvider(requireActivity()).get(EventViewModel::class.java)
        val deviceName = arguments?.getString(ARG_DEVICE_NAME, "") ?: ""
        val address = arguments?.getString(ARG_DEVICE_ADDRESS, "") ?: ""
        probeStatus = arguments?.getBoolean(ARG_DEVICE_PROBE_STATUS, false) ?: false
        mData.address = address
        mData.name = deviceName

        //信号 电量
        if (mBleViewModel.signalLevelCache.isNotEmpty() &&
            mBleViewModel.signalLevelCache.containsKey(mData.address)
        ) {
            val signal = mBleViewModel.signalLevelCache[mData.address] ?: -100
            viewBluetooth960.isSelected = signal != -100
            viewBluetooth1.isSelected = signal != -100 && probeStatus

            println("=====>  signalLevelCache viewBluetooth1  signal=$signal  probeStatus=$probeStatus ")
        }

        mDeviceStatusViewModel.initProfile(BleMessageViewModel.DEVICE_TYPE_TP960, mData.address)

        setListener()

        mBleViewModel.signalLevel.observeInFragment(this, Observer {
            if (it.values.last().containsKey(mData.address)) {
                println("=====>  setSignal $it")
                val signal = it.values.last()[mData.address] ?: -100
                viewBluetooth960.isSelected = signal != -100
                viewBluetooth1.isSelected = signal != -100

                println("=====>  signalLevel viewBluetooth1 signal=$signal ")
            }
        })

        mBleViewModel.signalLevelNoConnect.observeInFragment(this, Observer {
            if (it.values.last().containsKey(mData.address)) {
                println("=====>  setSignal  NoConnect $it")
                val signal = it.values.last()[mData.address] ?: -100
                viewBluetooth960.isSelected = signal != -100
                viewBluetooth1.isSelected = signal != -100 && probeStatus

                println("=====>  signalLevelNoConnect viewBluetooth1 signal=$signal  probeStatus=$probeStatus ")
            }
        })

        mBleViewModel.connectSuccess.observeInFragment(this, Observer {
            profileArray[0]?.let {
                mViewModel.setProfile(profileArray[0], 0, mData.address)
            }
            //电量
            mViewModel.getBoxBattery(mData.address)
            mViewModel.getProbeStatus(mData.address, 0)
            //历史温度
            mViewModel.getHistoryTemp(mData.address, 0)
        })

        if (!probeStatus) {
            mBleViewModel.notifyRefreshTempData.value?.clear()
        }
        mBleViewModel.notifyRefreshTempData.observeForever(Observer {
            if (it.isNotEmpty() && it.last().address == mData.address) {
                refreshTempDataCache.clear()
                refreshTempDataCache.addAll(it)
            }
        })

        mBleViewModel.notifyMsgData.observeInFragment(this, Observer { item ->
            val it = item.values.last()
            if (it.last().address == mData.address) {
                updateTemp(it)
            }
        })

        mDeviceStatusViewModel.profileListTP960Data.observeInFragment(this, Observer {
            println("=====> init profileListData $it")
            profileArray[0] = it[0]
            if (it[0].highTemperatureNum.isEmpty() && it[0].lowTemperatureNum.isEmpty()) {
                updateStatusView(TEMP_NONE, 1)
            } else if (it[0].singleTemp) {
                targetNum.text = LocalDataDao.INSTANCE.formatHighValueUnit(it[0])
                updateStatusView(TARGET_TEMP, 1)
            }

            meatText.text = it[0].name
            image1.setImageResource(it[0].iconRes)

            if (it.size() >= 2) {
                profileArray[1] = it[1]
                if (it[1].highTemperatureNum.isEmpty() && it[1].lowTemperatureNum.isEmpty()) {
                    updateStatusView(TEMP_NONE, 2)
                } else if (it[1].singleTemp) {
                    targetNum1.text = LocalDataDao.INSTANCE.formatHighValueUnit(it[1])
                    updateStatusView(TARGET_TEMP, 2)
                }
                meatText1.text = it[1].name
                image11.setImageResource(it[1].iconRes)
            }
            mBleViewModel.connectBluetooth(mData)

        })

        mDeviceStatusViewModel.profileData1.observeInFragment(this, Observer { item ->
            if (item.containsKey(mData.address)) {
                item[mData.address]?.let {
                    if (it.highTemperatureNum.isEmpty() && it.lowTemperatureNum.isEmpty()) {
                        updateStatusView(TEMP_NONE, 1)
                    } else if (it.singleTemp) {
                        targetNum.text = LocalDataDao.INSTANCE.formatHighValueUnit(it)

                        updateStatusView(TARGET_TEMP, 1)
                    }
                    meatText.text = it.name
                    image1.setImageResource(it.iconRes)
                    profileArray[0] = it
                    mViewModel.setProfile(it, 0, mData.address)
//                    mDeviceStatusViewModel.resetBlockWorking(0, mData.address)
//                    //历史温度
//                    mViewModel.getHistoryTemp(mData.address, 0)
                }
            }
        })

        mDeviceStatusViewModel.profileData2.observeInFragment(this, Observer { item ->
            if (item.containsKey(mData.address)) {
                item[mData.address]?.let {
                    if (it.highTemperatureNum.isEmpty() && it.lowTemperatureNum.isEmpty()) {
                        updateStatusView(TEMP_NONE, 2)
                    } else if (it.singleTemp) {
                        targetNum1.text = LocalDataDao.INSTANCE.formatHighValueUnit(it)
                        updateStatusView(TARGET_TEMP, 2)
                    }

                    meatText1.text = it.name
                    image11.setImageResource(it.iconRes)
                    profileArray[1] = it
                    mViewModel.setProfile(it, 1, mData.address)
//                    mDeviceStatusViewModel.resetBlockWorking(1, mData.address)
//                    //历史温度
//                    mViewModel.getHistoryTemp(mData.address, 1)
                }
            }
        })

        mViewModel.alarmClockText1.observeInFragment(this, Observer {
            val alarmData = it.values.last()
            if (alarmData.address == mData.address) {
                if (alarmData.alarmText.isNotEmpty() && alarmData.alarmText.contains(":")) {
                    val blockTimeText = alarmData.alarmText
                    val h = blockTimeText.split(":")[0].toInt()
                    val m = blockTimeText.split(":")[1].toInt()
                    val s = blockTimeText.split(":")[2].toInt()
                    if (h == 0 && m == 0 && s == 0) {
                        textBlock1.text = ""
                    } else {
                        if (h == 0 && m == 0) {
                            textBlock1.text = blockTimeText.split(":")[2]
                        } else if (h == 0) {
                            textBlock1.text =
                                blockTimeText.split(":")[1] + ":" + blockTimeText.split(":")[2]
                        } else {
                            textBlock1.text = blockTimeText
                        }
                    }
                } else {
                    textBlock1.text = ""
                }
            }
        })

        mDeviceStatusViewModel.alarmClock1.observeForever(Observer {
            if (it.containsKey(mData.address)) {
                mViewModel.alarmClockText1.postValue(hashMapOf<Long, EventAlarmTextData>().apply {
                    this[System.currentTimeMillis()] = EventAlarmTextData(
                        mData.address,
                        it[mData.address] ?: ""
                    )
                })
            }

//            if (it.containsKey(mData.address)) {
//                if (it.values.last().isNotEmpty()) {
//                    val blockTimeText = it[mData.address] ?: "0:0:0"
//                    val h = blockTimeText.split(":")[0].toInt()
//                    val m = blockTimeText.split(":")[1].toInt()
//                    val s = blockTimeText.split(":")[2].toInt()
//                    if (h == 0 && m == 0 && s == 0) {
//                        textBlock1.text = ""
//                    } else {
//                        if (h == 0 && m == 0) {
//                            textBlock1.text = blockTimeText.split(":")[2]
//                        } else if (h == 0) {
//                            textBlock1.text =
//                                blockTimeText.split(":")[1] + ":" + blockTimeText.split(":")[2]
//                        } else {
//                            textBlock1.text = blockTimeText
//                        }
//                    }
//                } else {
//                    textBlock1.text = ""
//                }
//            }
        })

        mViewModel.alarmClockText2.observeInFragment(this, Observer {
            val alarmData = it.values.last()
            if (alarmData.address == mData.address) {
                if (alarmData.alarmText.isNotEmpty() && alarmData.alarmText.contains(":")) {
                    val blockTimeText = alarmData.alarmText
                    val h = blockTimeText.split(":")[0].toInt()
                    val m = blockTimeText.split(":")[1].toInt()
                    val s = blockTimeText.split(":")[2].toInt()
                    if (h == 0 && m == 0 && s == 0) {
                        textBlock2.text = ""
                    } else {
                        if (h == 0 && m == 0) {
                            textBlock2.text = blockTimeText.split(":")[2]
                        } else if (h == 0) {
                            textBlock2.text =
                                blockTimeText.split(":")[1] + ":" + blockTimeText.split(":")[2]
                        } else {
                            textBlock2.text = blockTimeText
                        }
                    }
                } else {
                    textBlock2.text = ""
                }
            }
        })

        mDeviceStatusViewModel.alarmClock2.observeForever(Observer {
            if (it.containsKey(mData.address)) {
                mViewModel.alarmClockText2.postValue(hashMapOf<Long, EventAlarmTextData>().apply {
                    this[System.currentTimeMillis()] = EventAlarmTextData(
                        mData.address,
                        it[mData.address] ?: ""
                    )
                })
            }
        })

        //断开连接
        mBleViewModel.disconnectData.observeInFragment(this, Observer {
            if (it.first == mData.address) {
                viewBluetooth960.isSelected = false
                viewBluetooth1.isSelected = false

                println("=====>  disconnectData viewBluetooth1 false ")

                viewNoData.isVisible = true
                flow2.isInvisible = true
                viewNoData1.isVisible = true
                flow21.isInvisible = true

//                mDeviceStatusViewModel.resetBlockWorking(0, mData.address)
                refreshTempDataCache.clear()
            }
        })

        mBleViewModel.notifyTP960ProbeStatusForever.value?.clear()
        mBleViewModel.notifyTP960ProbeStatusForever.observeForever(Observer {
            if (it.isNotEmpty()) {
                mViewModel.notifyTP960ProbeStatus.postValue(it)
            }
        })

        mViewModel.notifyTP960ProbeStatus.observeInFragment(this, Observer {
            if (it.isNotEmpty()){
                it[mData.address]?.let { item ->
                    viewBluetooth1.isSelected = item.second == 1

                    println("=====>  notifyTP960ProbeStatus viewBluetooth1  ${item.second == 1} ")
                    if (item.second != 1) {
                        viewNoData.isVisible = true
                        flow2.isInvisible = true
                        viewNoData1.isVisible = true
                        flow21.isInvisible = true
                    }
                }
            }
        })

//        mBleViewModel.notifyTP960ProbeBattery.value?.clear()
        mBleViewModel.notifyTP960ProbeBattery.observeInFragment(this, Observer {
            it[mData.address]?.let { item ->
                setProbeBatteryLevel(item.second, item.first)
            }
        })

        mBleViewModel.notifyTP960BoxBattery.observeInFragment(this, Observer {
            if (it.isNotEmpty() && it.keys.last()==mData.address){
                setBatteryLevel(it[mData.address]?:0)
            }
        })

        mDeviceStatusViewModel.needTimeClock1.observeInFragment(this, Observer {
            println("123->  needTimeClock1 = ${it.toString()}")
            if (it.containsKey(mData.address)) {
                if (it.values.last().isNotEmpty()) {
                    val blockTimeText = it[mData.address] ?: ""

                    viewForecastTime.text = LocalDataDao.INSTANCE.formatNeedTime(blockTimeText)
                } else {
                    viewForecastTime.text = LocalDataDao.INSTANCE.formatNeedTime()
                }
            }
        })

//        mViewModel.getHistoryTempByCache.observeInFragment(this, Observer {
//            mBleViewModel.getTempHistoryByCache(address, deviceName)
//        })

        mBleViewModel.probeTempComputeTime.observeForever(Observer {
            mViewModel.compute2TempTime.postValue(it.second)
            println("123->  compute2TempTime=${it.second}")
            //剩余时间倒计时
            val address = it.second.keys.last().split("_")[0]
            if (mData.address == address) {
                val clockIndex = it.second.keys.last().split("_")[1].toIntOrNull() ?: -1
                if (it.second.values.last().split(":").size > 2) {
                    println("123->  mDeviceStatusViewModel tp960  startTimeClock ")
                    mDeviceStatusViewModel.startTimeClock(
                        address,
                        clockIndex,
                        it.second.values.last()
                    )
                } else {
                    if (mDeviceStatusViewModel.isBlockTimeWorking(clockIndex, address)) {
                        mDeviceStatusViewModel.resetBlockTimeWorking(clockIndex, address)
                    }
                }
            }
        })

        mViewModel.compute2TempTime.observeInFragment(this, Observer {
            if (it.isNotEmpty()) {
                it.keys.forEach { itKey ->
                    val address = itKey.split("_")[0]
                    if (mData.address == address) {
                        var forecastTime = it[itKey] ?: ""
                        when (itKey.split("_")[1].toIntOrNull()) {
                            0 -> {
//                                viewForecastTime.text = forecastTime
                                viewForecastTime.text =
                                    LocalDataDao.INSTANCE.formatNeedTime(forecastTime)
                            }
                            1 -> {
//                                viewForecastTime1.text = forecastTime
                                viewForecastTime1.text =
                                    LocalDataDao.INSTANCE.formatNeedTime(forecastTime)
                            }
                            else -> {

                            }
                        }
                    }
                }
            }
        })


        mDeviceStatusViewModel.resetProfile.observeInFragment(this, Observer {
            mViewModel.setProfile(
                LocalDataDao.INSTANCE.getProfile1(mData.address),
                0, mData.address
            )
        })
    }

    private fun setBatteryLevel(level: Int) {
        when (level) {
            0 -> {
//                if (viewBoxPower.isVisible) {
//                    viewBoxPower.isVisible = false
//                }
                viewBoxPower.setImageResource(R.drawable.battery_full)

            }
            1 -> {
                viewBoxPower.setImageResource(R.drawable.battery_low)
                parentView.snackBar(
                    getString(R.string.text_booster_battery_low), getString(R.string.text_ok)
                ) {

                }
            }
            else -> viewBoxPower.setImageResource(R.drawable.battery_full)
        }
    }

    private fun setProbeBatteryLevel(level: Float, probeIndex: Int) {
        println("======>  setProbeBatteryLevel  level=$level  probeIndex=$probeIndex")
        when {
            level < 2.3 -> {
                when (probeIndex) {
                    0 -> probePower1.setImageResource(R.drawable.battery_null)
                    1 -> probePower2.setImageResource(R.drawable.battery_null)
                }
                parentView.snackBar(
                    getString(R.string.text_probe_battery_low), getString(R.string.text_ok)
                ) {

                }
            }
            level in 2.3..2.45 -> {
                when (probeIndex) {
                    0 -> probePower1.setImageResource(R.drawable.battery_low)
                    1 -> probePower2.setImageResource(R.drawable.battery_low)
                }
            }
            level in 2.45..2.6 -> {
                when (probeIndex) {
                    0 -> probePower1.setImageResource(R.drawable.battery_mid)
                    1 -> probePower2.setImageResource(R.drawable.battery_mid)
                }
            }
            level > 2.6 -> {
                when (probeIndex) {
                    0 -> probePower1.setImageResource(R.drawable.battery_full)
                    1 -> probePower2.setImageResource(R.drawable.battery_full)
                }
            }
            else -> when (probeIndex) {
                0 -> probePower1.setImageResource(R.drawable.battery_full)
                1 -> probePower2.setImageResource(R.drawable.battery_full)
            }
        }
    }

    /**
     * 更新温度等状态
     */
    private fun updateTemp(itList: MutableList<ProbeBean>) {
        probeArray.clear()
        probeArray.addAll(itList)
        if (itList.size == 2) {
            viewNum.text = LocalDataDao.INSTANCE.convertUnitNum(itList[0].temperatureNUm)
            viewUnit.text = LocalDataDao.INSTANCE.getTempUnit()
            viewNum1.text = LocalDataDao.INSTANCE.convertUnitNum(itList[1].temperatureNUm)
            viewUnit1.text = LocalDataDao.INSTANCE.getTempUnit()
            if (itList[0].temperatureNUm.isNotEmpty()) {
                viewNoData.isVisible = false
                flow2.isInvisible = false
            } else {
                viewNoData.isVisible = true
                flow2.isInvisible = true
            }
            if (itList[1].temperatureNUm.isNotEmpty()) {
                viewNoData1.isVisible = false
                flow21.isInvisible = false
            } else {
                viewNoData1.isVisible = true
                flow21.isInvisible = true
            }
        } else if (itList.size == 1) {
            viewNum.text = LocalDataDao.INSTANCE.convertUnitNum(itList[0].temperatureNUm)

            viewUnit.text = LocalDataDao.INSTANCE.getTempUnit()
            if (itList[0].temperatureNUm.isNotEmpty()) {
                viewNoData.isVisible = false
                flow2.isInvisible = false
            } else {
                viewNoData.isVisible = true
                flow2.isInvisible = true
            }
        }
        for (i in 0 until itList.size) {
            val profile = when (i) {
                0 -> {
                    LocalDataDao.INSTANCE.getProfile1(itList[i].address)
                }
                1 -> {
                    LocalDataDao.INSTANCE.getProfile2(itList[i].address)
                }
                else -> {
                    ProfileBean(
                        "MEAT",
                        "", mutableListOf(), singleTemp = false
                    )
                }
            }
            if (profile.highTemperatureNum.isNotEmpty()) {
                profile.highTemperatureNum.toIntOrNull()?.let {
                    if (itList[i].temperatureNUm.toInt() >= it
                    ) {
                        println("======> resetBlockWorking   profile=$profile")
//                        mDeviceStatusViewModel.resetBlockWorking(0, mData.address)
                    }
                }
            }
        }

    }


    private fun updateStatusView(status: Int, viewPosition: Int) {
        when (status) {
            TARGET_TEMP -> {
                when (viewPosition) {
                    1 -> {
                        image2.isInvisible = false
                        targetNum.isInvisible = false
                        image3.isInvisible = true
                        targetLowNum.isInvisible = true
                        textTarget.isInvisible = false
                    }
                    2 -> {
                        image21.isInvisible = false
                        targetNum1.isInvisible = false
                        image31.isInvisible = true
                        targetLowNum1.isInvisible = true
                        textTarget1.isInvisible = false
                    }
                }
            }
            TEMP_NONE -> {
                when (viewPosition) {
                    1 -> {
                        image2.isInvisible = true
                        targetNum.isInvisible = true
                        image3.isInvisible = true
                        targetLowNum.isInvisible = true
                        textTarget.isInvisible = true
                    }
                    2 -> {
                        image21.isInvisible = true
                        targetNum1.isInvisible = true
                        image31.isInvisible = true
                        targetLowNum1.isInvisible = true
                        textTarget1.isInvisible = true
                    }
                }
            }
        }
    }


    private fun setListener() {
        viewNum.typeface = AppContext.get().getTypeFace()
        viewNum1.typeface = AppContext.get().getTypeFace()
        backButton.setOnClickListener {
            requireActivity().onBackPressed()
        }

        mainMenuButton.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    TP960SettingFragment.newInstance(mData.address)
                )
                .addToBackStack("main")
                .commit()
        }

        flow1.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    SetProfileFragment.newInstance(
                        1, meatText.text.toString(),
                        BleMessageViewModel.DEVICE_TYPE_TP960, mData.address
                    )
                )
                .addToBackStack("main")
                .commit()
        }

        flow11.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    SetProfileFragment.newInstance(
                        2, meatText1.text.toString(),
                        BleMessageViewModel.DEVICE_TYPE_TP960, mData.address
                    )
                )
                .addToBackStack("main")
                .commit()
        }

        viewTime1.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    BlockTimerFragment.newInstance(0, mData.address)
                )
                .addToBackStack("main")
                .commit()
        }

        viewTime11.setOnClickListener {
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    BlockTimerFragment.newInstance(1, mData.address)
                )
                .addToBackStack("main")
                .commit()
        }


        viewTrend1.setOnClickListener {
            val tempNumList = arrayListOf<String>()
            probeArray.forEach {
                tempNumList.add(it.temperatureNUm)
            }
            if (tempNumList.isEmpty()) {
                tempNumList.add("")
            }
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    TempGraphFragment.newInstance(
                        tempNumList, 0,
                        BleMessageViewModel.DEVICE_TYPE_TP960, mData.address
                    )
                )
                .addToBackStack("main")
                .commit()
        }

        viewTrend11.setOnClickListener {
            val tempNumList = arrayListOf<String>()
            probeArray.forEach {
                tempNumList.add(it.temperatureNUm)
            }
            if (tempNumList.isEmpty()) {
                tempNumList.add("")
            }
            parentFragmentManager.beginTransaction()
                .setCustomAnimations(
                    R.anim.translate_into, R.anim.translate_out,
                    R.anim.translate_into_pop, R.anim.translate_out_exit
                )
                .add(
                    R.id.mainContent,
                    TempGraphFragment.newInstance(
                        tempNumList, 1,
                        BleMessageViewModel.DEVICE_TYPE_TP960, mData.address
                    )
                )
                .addToBackStack("main")
                .commit()
        }

    }


}